package com.ruoyi.project.module.location.controller;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.shiro.service.PasswordService;
import com.ruoyi.project.module.purchases.domain.Purchases;
import com.ruoyi.project.system.dept.service.DeptServiceImpl;
import com.ruoyi.project.system.dept.service.IDeptService;
import com.ruoyi.project.system.menu.domain.Menu;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.constant.BusinessType;
import com.ruoyi.project.module.location.domain.Location;
import com.ruoyi.project.module.location.service.ILocationService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;

/**
 * 货物位置 信息操作处理
 * 
 * @author lcx
 * @date 2018-10-08
 */
@Controller
@RequestMapping("/module/location")
public class LocationController extends BaseController
{
    private String prefix = "module/location";

    private String prefix1 = "module/purchases";


    @Autowired
	private ILocationService locationService;
	@Autowired
	private IDeptService deptService;
	
	@RequiresPermissions("module:location:view")
	@GetMapping()
	public String location(ModelMap mmap)
	{
		mmap.put("depList", deptService.selectDeptAll());
		return prefix + "/location";
	}
	
	/**
	 * 查询货物位置列表
	 */
	@RequiresPermissions("module:location:list")
	@GetMapping("/list")
	@ResponseBody
	public  List<Location> list(Location location)
	{
		if("100".equals(location.getCompanyId())){
		location.setCompanyId("");
		}
        List<Location> list = locationService.selectLocationList(location);
		return list;
	}
	
	/**
	 * 新增货物位置
	 */
	@GetMapping("/add/{parentId}")
	public String add(@PathVariable("parentId") Integer parentId, ModelMap mmap)
	{
		Location location = null;
		if (0L != parentId)
		{
            location = locationService.selectLocationById(parentId);
		}
		else
		{
			location = new Location();
			location.setLocationId(0);
			location.setLocationName("主货柜");
		}
		mmap.put("location", location);


		return prefix + "/add";
	}
	
	/**
	 * 新增保存货物位置
	 */
	@RequiresPermissions("module:location:add")
	@Log(title = "货物位置", action = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public List<Integer> addSave(Location location)
	{
		return locationService.insertLocation(location);

		/*return toAjax(1);*/
	}

	/**
	 * 修改货物位置
	 */
	@GetMapping("/edit/{locationId}")
	public String edit(@PathVariable("locationId") Integer locationId, ModelMap mmap)
	{
		Location location = locationService.selectLocationById(locationId);
		mmap.put("location", location);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存货物位置
	 */
	@RequiresPermissions("module:location:edit")
	@Log(title = "货物位置", action = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public List<Integer> editSave(Location location)
	{		
		return locationService.updateLocation(location);
		//return toAjax(1);
	}
	
	/**
	 * 删除货物位置
	 */
    @RequiresPermissions("module:location:remove")
    @Log(title = "货物位置", action = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
	public AjaxResult remove(String id)
	{

        /*if (locationService.selectCountLocationByParentId(id) > 0)
        {
            return error(1, "存在子菜单,不允许删除");
        }*/
        //是否有货物
       /* if (locationService.selectCountRoleMenuByMenuId(locationId) > 0)
        {
            return error(1, "菜单已分配,不允许删除");
        }*/


	    return toAjax(locationService.deleteLocationByIds(id));
	}

	/**
	 * 加载所有菜单列表树
	 */
	@GetMapping("/menuTreeData")
	@ResponseBody
	public List<Map<String, Object>> menuTreeData()
	{
		List<Map<String, Object>> tree = locationService.locationTreeData();
		return tree;
	}

	/**
	 * 选择菜单树
	 */
	@GetMapping("/selectMenuTree/{menuId}")
	public String selectMenuTree(@PathVariable("menuId") Integer locationId, ModelMap mmap)
	{
		mmap.put("treeName", locationService.selectLocationById(locationId).getLocationName());
		return prefix + "/tree";
	}

	/**
	 * 校验仓位名称
	 */
	@PostMapping("/checkLocationNameUnique")
	@ResponseBody
	public String checkLocationNameUnique(Location location)
	{
		String uniqueFlag = "0";
		if (StringUtils.isNotNull(location))
		{
			uniqueFlag = locationService.checkLocationNameUnique(location);
		}
		return uniqueFlag;
	}

    /**
     * 校验仓位id
     */
    @PostMapping("/checkLocationIdUnique")
    @ResponseBody
    public String checkLocationIdUnique(String smLocationId)
    {
        String uniqueFlag = "0";
        if (StringUtils.isNotNull(smLocationId) && smLocationId.length() > 10)
        {
            String locationId = smLocationId.substring(10);
            Location location = new Location();
            location.setLocationId(Integer.valueOf(locationId));
            location.setLocationType("F");
            if(locationService.selectLocationList(location).size() != 0){
                uniqueFlag = "1";
            }
        }
        return uniqueFlag;
    }

	/**
	 * @Author lcx
	 * @Date 2018/10/12 15:26
	 */
	@Log(title = "弹出扫码上架窗口", action = BusinessType.OTHER)
	@GetMapping("/toSweepCodeLocation")
	public String toSweepCodeLocation(ModelMap mmap,String flag){
		// "F" 货框
		Location location = new Location();
		location.setLocationType("F");
		location.setVisible("0");
		List<Location> locationList = locationService.selectCountLocationByType(location);
		mmap.put("flag", flag);
		mmap.put("locationList", locationList);
		return prefix + "/sweepCodeLocation";
	}
    /**
     * @Author lcx
     * @Date 2018/10/12 15:26
     */
    @Log(title = "弹出扫码下架窗口", action = BusinessType.OTHER)
    @GetMapping("/toSweepCodeLocationX")
    public String toSweepCodeLocationX(ModelMap mmap,String flag){
        mmap.put("flag", flag);
        return prefix + "/sweepCodeLocationX";
    }

	/**
	 * @Author lcx
	 * @Date 2018/10/12 15:26
	 */
	@Log(title = "弹出扫码移货窗口", action = BusinessType.OTHER)
	@GetMapping("/toSweepCodeLocationY")
	public String toSweepCodeLocationY(ModelMap mmap,String flag){
		// "F" 货框
		Location location = new Location();
		location.setLocationType("F");
		location.setVisible("0");
		List<Location> locationList = locationService.selectCountLocationByType(location);
		mmap.put("flag", flag);
		mmap.put("locationList", locationList);
		return prefix + "/sweepCodeLocationY";
	}

	/**
	 * @Author lcx
	 * @Date 2018/10/26 9:10
	 */
	@Log(title = "弹出捡货窗口", action = BusinessType.OTHER)
	@GetMapping("/toSweepCodeLocationJ")
	public String toSweepCodeLocationJ(ModelMap mmap,String flag){
		/*// "F" 货框
		Location location = new Location();
		location.setLocationType("F");
		location.setVisible("0");
		List<Location> locationList = locationService.selectCountLocationByType(location);
		mmap.put("flag", flag);
		mmap.put("locationList", locationList);*/
		return prefix + "/sweepCodeLocationJ";
	}


	/**
	 * @Author lcx
	 * @Date /2018/10/12/ 11:04
	 * 功能 stockIds 为特殊判断了的 此接口只支持 海外仓发货99数量一下的 扫码盘点 现需求不会很多 如果需要修改 请参考 purchases.print_label.html 下40行的方式 与本操作sql
	 * */
	@Log(title = "扫码上架", action = BusinessType.UPDATE)
	@PostMapping("/sweepCodeLocation")
	@ResponseBody
	public List<String> SweepCodeLocation(@RequestParam(value="smIds[]") String[] smIds,
										  @RequestParam(value="locationIds") String locationId) {
		return locationService.updateLocationAsSweepCodeLocation(smIds,locationId,"SweepCodeLocation");
	}

    @Log(title = "扫码下架", action = BusinessType.UPDATE)
    @PostMapping("/sweepCodeLocationX")
    @ResponseBody
    public List<String> SweepCodeLocationX(@RequestParam(value="smIds[]") String[] smIds) {
        return locationService.updateLocationAsSweepCodeLocationX(smIds);
    }

	@Log(title = "扫码移货", action = BusinessType.UPDATE)
	@PostMapping("/sweepCodeLocationY")
	@ResponseBody
	public List<String> SweepCodeLocationY(@RequestParam(value="smIds[]") String[] smIds,
										  @RequestParam(value="locationIds") String locationId) {
		return locationService.updateLocationAsSweepCodeLocation(smIds,locationId,"SweepCodeLocationY");
	}

	@Log(title = "捡货", action = BusinessType.UPDATE)
	@PostMapping("/sweepCodeLocationJ")
	@ResponseBody
	public Map<String, List<String>> SweepCodeLocationJ(@RequestParam(value="mainSKU") String mainSKU) {

		Map<String, List<String>> listMap = locationService.selectLocationAsMainSKU(mainSKU.split("，"));
		return listMap;
	}

	/**
	 * @Author lcx
	 * @Date 2018/10/15 9:49
     * 批量打印-货架
	 */
	@GetMapping("/print_label/{ids}/{createDates}")
	public String print_label(@PathVariable("ids") String ids,
							  @PathVariable("createDates") String createDates,
							  ModelMap mmap) {

		List <Location> locationList = locationService.selectLocationAsPrintLabel(ids);
		mmap.put("locationList", locationList);

		return prefix + "/print_label";
	}

    /**
     * @Author lcx
     * @Date 2018/10/15 9:49
     * 批量打印-商品
     */
    @GetMapping("/print_labelAsPrintId/{ids}/{createDates}")
    public String print_labelAsPrintId(@PathVariable("ids") String ids,
                              @PathVariable("createDates") String createDates,
                              ModelMap mmap) {

        List<Purchases> purchasesList = locationService.selectLocationListAsPringId(ids);
        mmap.put("purchasesList", purchasesList);

        return prefix + "/print_label_product";
    }


}
